package com.haieros.www.purerunshowsystem;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.haieros.www.purerunshowsystem.utils.VideoViewUtil;

import java.util.ArrayList;


/**
 * Created by admin on 2017/11/21.
 */

public class VideoViewDemoActivity extends Activity {
    VideoViewUtil videoView;
    private ArrayList<String> mlistStrUrl;
    private int position;
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videoview_activity_layout);
        Intent intent=getIntent();
        VideoModel videoModel=(VideoModel) intent.getSerializableExtra("data");
        position = intent.getIntExtra("position",0);
        mlistStrUrl = intent.getStringArrayListExtra("paths");
        //本地的视频  需要在手机SD卡根目录添加一个 fl1234.mp4 视频
        String videoUrl1 = videoModel.getPath();

        Uri uri = Uri.parse(videoModel.getPath());

        videoView = (VideoViewUtil) this.findViewById(R.id.videoviewDemo);

        //设置视频控制器
        videoView.setMediaController(new android.widget.MediaController(this));

        //播放完成回调
        videoView.setOnCompletionListener( new MyPlayerOnCompletionListener());

        //设置视频路径
        videoView.setVideoURI(uri);

        //开始播放视频
        videoView.start();
    }

    class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {
        @Override
        public void onCompletion(MediaPlayer mp) {
            position += 1;
            if (position >= mlistStrUrl.size()){
                position = 0;
            }
            videoView.setVideoURI(Uri.parse(mlistStrUrl.get(position)));
            videoView.start();
        }
    }
}
